package cn.lzj.pet.controller;

import cn.lzj.basic.util.AjaxResult;
import cn.lzj.basic.util.PageList;
import cn.lzj.pet.domain.SearchMasterMsg;
import cn.lzj.pet.dto.MsgDto;
import cn.lzj.pet.query.SearchMasterMsgQuery;
import cn.lzj.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：添加或修改
     *
     * @param searchMasterMsg 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() != null) {
                searchMasterMsgService.update(searchMasterMsg);
            } else {
                searchMasterMsgService.add(searchMasterMsg);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.util().setmessage("噢哟，系统报错了，我们正在殴打程序员！！!");
        }
    }

    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.util().setmessage("噢哟，系统报错了，我们正在殴打程序员！！!");
        }
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult removeList(@RequestBody List<Long> ids) {
        try {
            searchMasterMsgService.removeList(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.util().setmessage("噢哟，系统报错了，我们正在殴打程序员！！!");
        }
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    public SearchMasterMsg queryById(@PathVariable("id") Long id) {
        return searchMasterMsgService.queryById(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping
    public List<SearchMasterMsg> queryAll() {
        return searchMasterMsgService.queryAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 接口：发布寻主的接口
     *
     * @param searchMasterMsg,request
     * @return AjaxResult 响应给前端
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        try {
            searchMasterMsgService.publish(searchMasterMsg, request);
            return AjaxResult.util();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.util().setmessage("噢哟，系统报错了，我们正在殴打程序员！！!");
        }
    }
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {

        return searchMasterMsgService.user(query, request);

    }
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query) {

        return searchMasterMsgService.queryPage(query);

    }
    @PostMapping("/over")
    public PageList<SearchMasterMsg> over(@RequestBody SearchMasterMsgQuery query) {

        return searchMasterMsgService.queryPage(query);

    }
    @PostMapping("/searchMasterPool")
    public PageList<SearchMasterMsg> searchMasterPool(@RequestBody SearchMasterMsgQuery query) {

        return searchMasterMsgService.queryPage(query);

    }
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        query.setState(1);
        return searchMasterMsgService.toHandle(query, request);

    }
    @GetMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId) {

        try {
             searchMasterMsgService.reject(msgId);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setsuccess(false).setmessage("【500】服务器繁忙，请稍后再试");
        }
    }
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody MsgDto msgDto, HttpServletRequest request) {

        try {
            searchMasterMsgService.accept(msgDto);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setsuccess(false).setmessage("【500】服务器繁忙，请稍后再试");
        }
    }
}
